package org.fanrui.hrm.web.controller;

import org.fanrui.hrm.query.OperationLogQuery;
import org.fanrui.hrm.service.IOperationLogService;
import org.fanrui.hrm.domain.OperationLog;

import com.baomidou.mybatisplus.plugins.Page;
import org.fanrui.hrm.util.AjaxResult;
import org.fanrui.hrm.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/operationLog")
public class OperationLogController {
    @Autowired
    public IOperationLogService operationLogService;

    /**
     * 保存和修改公用的
     *
     * @param operationLog 传递的实体
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult save(@RequestBody OperationLog operationLog) {
        try {
            if (operationLog.getId() != null) {
                operationLogService.updateById(operationLog);
            } else {
                operationLogService.insert(operationLog);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            operationLogService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public OperationLog get(@PathVariable("id") Long id) {
        return operationLogService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<OperationLog> list() {

        return operationLogService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public PageList<OperationLog> json(@RequestBody OperationLogQuery query) {
        Page<OperationLog> page = new Page<OperationLog>(query.getPage(), query.getRows());
        page = operationLogService.selectPage(page);
        return new PageList<OperationLog>(page.getTotal(), page.getRecords());
    }
}
